package com.woniuxy.service.impl;

import com.woniuxy.dao.OrderDao;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.utils.ResponseEntity;
import com.woniuxy.service.OrderService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 马宇航
 * @Description: 使用了eureka服务后，我们实现服务的轮询
 * @DateTime: 22/12/06/0006 10:40
 * @Component: 成都蜗牛学苑
 **/
//@Service
public class EurekaOrderServiceImpl implements OrderService {
    static int times =0;
    @Autowired
    OrderDao orderDao;
    @Autowired
    DiscoveryClient discoveryClient;
    @Override
    public ResponseEntity generateOrder(Integer pid, Integer num) {
        //获取所有服务器url地址的集合
        List<ServiceInstance> urls = this.allURL();
        times++;
        ServiceInstance serviceInstance = urls.get(times%urls.size());
        URI uri = serviceInstance.getUri();
        //使用入门的微服务调用方案
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity productResp = restTemplate.getForObject(uri.toString()+"/product/" + pid, ResponseEntity.class);
        //写业务代码
        Product product = new Product();
        //处理如果获取不到数据
        if (Integer.parseInt(productResp.get(ResponseEntity.ResponseStatus.CODE.value())+"")==HttpStatus.INTERNAL_SERVER_ERROR.value()){
            return productResp;
        }
        Map map= (Map) productResp.get(ResponseEntity.ResponseStatus.DATA.value());
        try {
            //想办法把LinkedHashMap转换为Product对象
            BeanUtils.populate(product,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        Order order = new Order();
        //用户信息假数据
        order.setUid(1);
        order.setUsername("马蜂窝");
        order.setPid(product.getPid());
        order.setPname(product.getName());
        order.setPrice(product.getPrice());
        //存储购买的数量
        order.setNumber(num);
        Order returnOrder = null;
        //如果订单入库成功，应该扣减库存
        if (product.getStock()-num>=0) {//商品扣减后必须大于等于0才能够扣减商品
            returnOrder = orderDao.save(order);
            product.setStock(product.getStock()-num);
            restTemplate.put(uri.toString(), product);
        }else{
           return new ResponseEntity(HttpStatus.NOT_MODIFIED.value(),"商品卖完了。");
        }

        return new ResponseEntity(returnOrder);
    }
    //服务发现
    public  List<ServiceInstance> allURL(){
        List<ServiceInstance> instances = discoveryClient.getInstances("MYH-PRODUCT");
        return instances;
    }
}
